<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * The project “dev”
 * File name “Acl_model.php”
 *
 * User: znx
 * Company: 思议创想
 * Q  Q: 865303386
 * Site: www.siyi360.com
 *
 * Date: 2017-02-09
 * Time: 15:25
 */
class Acl_model extends CI_Model {
    /**
     * menu_parent_get
     * 获取管理一级菜单作为权限资源分组
     *
     * @param int $menu_id 菜单id
     * @return mixed
     */
    public function menu_parent_get($menu_id=null)
    {
        if ($menu_id) $this->db->where('menu_id', $menu_id);
        $this->db->where('menu_parentid', 0);
        $this->db->order_by('menu_sort DESC');
        $query = $this->db->get('admin_menu');

        return $query->result_array();
    }

    /**
     * get_all_acl
     * 获取所有权限数据
     *
     * @return mixed
     */
    public function get_all_acl()
    {
        return $this->db->order_by('acl_id ASC')->get('admin_acl')->result_array();
    }

    /**
     * get_single_acl
     * 获取指定id的单条权限数据
     *
     * @param int $acl_id 权限id
     * @return mixed
     */
    public function get_single_acl($acl_id)
    {
        $table = $this->db->dbprefix('admin_acl');
        return $this->db
            ->query('select * from ' . $table . ' where acl_id=' . $acl_id)
            ->row_array();
    }

    /**
     * acl_save
     * 保存权限资源到数据库
     *
     * @return array
     */
    public function acl_save()
    {
        // 获取表单数据
        $form = $this->input->post();

        $data = array(
            'acl_name' => $form['acl_name'],
            'acl_group' => $form['acl_group'],
            'acl_controller' => $form['acl_controller'],
            'acl_action' => implode('|', $form['acl_action'])
        );

        // 判断主键ID是否存在，存在则表示更新数据
        if (!array_key_exists('acl_id', $form)) {
            // 新增数据
            $req = $this->db->insert('admin_acl', $data);
        } else {
            // 更新数据
            $req = $this->db->where('acl_id', $form['acl_id'])->update('admin_acl', $data);
        }

        if ($req) {
            return array('status'=>1, 'msg'=>'操作成功', 'result'=>array('jumpUrl'=>site_url('system/Acl/index')));
        } else {
            return array('status'=>0, 'msg'=>'操作失败', 'result'=>'');
        }
    }

    /**
     * acl_delete
     * 删除权限资源
     * @return array
     */
    public function acl_delete()
    {
        $acl_id = $this->input->get('acl_id');

        $req = $this->db->where('acl_id',$acl_id)->delete('admin_acl');
        if ($req) {
            return array('status'=>1, 'msg'=>'操作成功！', 'result'=>'');
        } else {
            return array('status'=>0, 'msg'=>'操作失败！', 'result'=>'');
        }
    }
}